package com.kinghood.authoritycenter.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kinghood.authoritycenter.common.model.dao.authority.AuthorityApiDO;
import com.kinghood.authoritycenter.common.model.dto.authority.AddApiReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.ApiGetListReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.ApiQueryPageReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.UpdateApiReqDTO;
import com.kinghood.authoritycenter.common.util.UserUtil;
import com.kinghood.authoritycenter.dao.authority.AuthorityApiMapper;
import com.kinghood.authoritycenter.service.IAuthorityApiService;
import com.kinghood.infrastructure.model.KingHoodExceptionUtil;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.authoritycenter.common.model.dto.auth.UserDTO;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.sharding.model.KingHoodPageUtil;
import com.kinghood.sharding.model.KingPageResult;
import com.kinghood.sharding.model.PageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统api接口表 服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2023-12-18
 */
@Transactional(rollbackFor = Exception.class)
@Slf4j
@Service
public class AuthorityApiServiceImpl extends ServiceImpl<AuthorityApiMapper, AuthorityApiDO> implements IAuthorityApiService {

    @Override
    public PageResponseVO<AuthorityApiDO> queryPage(ApiQueryPageReqDTO in) {

        Page<AuthorityApiDO> page = this.baseMapper.queryPage(KingHoodPageUtil.getPage(in, in), in);
        return KingPageResult.getResult(page);
    }

    @Override
    public AuthorityApiDO addApi(AddApiReqDTO in) {

        long count = new LambdaQueryChainWrapper<>(this.getBaseMapper())
                .eq(AuthorityApiDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .eq(AuthorityApiDO::getUrl, in.getUrl())
                .count();
        KingHoodExceptionUtil.trueThrows(count > 0, "url存在");

        AuthorityApiDO one = new AuthorityApiDO();
        BeanUtils.copyProperties(in, one);
        Date now = new Date();
        one.setCreateTime(now);
        one.setUpdateTime(now);
        one.setCreateUserId(UserUtil.getUserId());
        one.setUpdateUserId(UserUtil.getUserId());
        one.setId(IdGenUtil.get().nextId());
        this.save(one);

        log.info("新增api：{}，操作人：{}", in, UserUtil.getUser());
        return one;
    }


    @Override
    public AuthorityApiDO getApi(Long id) {
        return this.getById(id);
    }

    @Override
    public AuthorityApiDO updateApi(UpdateApiReqDTO in) {

        AuthorityApiDO one = getApi(in.getId());
        KingHoodExceptionUtil.isNull(one, "api不存在");

        BeanUtils.copyProperties(in, one);
        UserDTO userDetail = UserUtil.getUser();
        one.setUpdateTime(new Date());
        one.setUpdateUserId(userDetail.getId());
        this.updateById(one);

        log.info("修改api：{}，操作人：{}", in, userDetail);
        return one;
    }

    @Override
    public Boolean deleteApiBatch(List<Long> idList) {
        KingHoodExceptionUtil.isEmpty(idList, "请选择要删除的api");

        UserDTO user = UserUtil.getUser();
        boolean update = new LambdaUpdateChainWrapper<>(this.getBaseMapper())
                .in(AuthorityApiDO::getId, idList)
                .set(AuthorityApiDO::getIsDelete, CommonConstants.IS_DELETE_Y)
                .set(AuthorityApiDO::getUpdateUserId, UserUtil.getUserId())
                .set(AuthorityApiDO::getUpdateTime, new Date())
                .update();

        log.info("删除api：{}，结果：{}，操作人：{}", idList, update, user);
        return update;

    }

    @Override
    public List<AuthorityApiDO> getList(ApiGetListReqDTO in) {

        return new LambdaQueryChainWrapper<>(this.getBaseMapper())
                .eq(StringUtils.isNotBlank(in.getApiName()), AuthorityApiDO::getApiName, in.getApiName())
                .eq(AuthorityApiDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .eq(AuthorityApiDO::getStatus, CommonConstants.ENABLE)
                .orderByAsc(AuthorityApiDO::getCreateTime)
                .list();
    }


}
